package v1

import (
	"crypto/md5"
	"encoding/hex"
	"gateway-api/controllers"
	"gateway-api/tools"
	"os"
	"path"
	"strconv"
	"strings"

	"github.com/gin-gonic/gin"
)

/*
 +----------------------------------------------------------------------
 + Title        : ImageController
 + Author       : huwl
 + Version      : 2.0.0
 + Initial-Time : 2020-11-23
 + Last-time    : 这个文件最后修改的时间 + 修改人的名称
 + Desc         : 图片控制层
 +----------------------------------------------------------------------
*/
type ImageController struct {
	controllers.BaseController
}

/**
 * @description 本地上传单张图片
 * @author huwl
 * @date 2025/08/05 19:43:19
 */
func (control ImageController) LocalSingleUploadImage(ctx *gin.Context) {
	image, err := ctx.FormFile("file")
	if err != nil {
		control.BaseError(ctx, control.WithMessage("请选择上传图片"))
		return
	}

	thumbs := []map[string]string{}
	water := map[string]string{}

	// thumbs = append(thumbs, map[string]string{
	// 	"width":  "200",
	// 	"height": "200",
	// })
	result, err := (&tools.SingleUploadImage{}).SingleUploadImage(ctx, image, thumbs, water)
	if err != nil {
		control.BaseError(ctx, control.WithMessage(err.Error()))
		return
	}

	control.BaseSuccess(ctx, control.WithData(gin.H{
		"image_url":          result["image_url"],
		"absolute_image_url": result["absolute_image_url"],
	}), control.WithMessage("上传图片成功"))
}

/**
 * @description oss上传单张图片
 * @author huwl
 * @date 2025/08/05 19:43:06
 */
func (control ImageController) OssSingleImageUpload(ctx *gin.Context) {
	image, err := ctx.FormFile("file")
	if err != nil {
		control.BaseError(ctx, control.WithMessage("上传图片错误"))
		return
	}

	thumbs := []map[string]string{}
	water := map[string]string{}

	thumbs = append(thumbs, map[string]string{
		"width":  "200",
		"height": "200",
	})

	result, err := (&tools.OssSingleUploadImage{}).OssSingleImageUpload(ctx, image, "test", thumbs, water)
	if err != nil {
		control.BaseError(ctx, control.WithMessage(err.Error()))
		return
	}

	control.BaseSuccess(ctx, control.WithData(gin.H{
		"image_url":          result["image_url"],
		"absolute_image_url": result["absolute_image_url"],
		"thumb_images":       result["thumb_images"],
	}), control.WithMessage("上传图片成功"))
}

/**
 * @description 批量图片上传
 * @author huwl
 * @date 2025/08/05 19:42:31
 */
func (control ImageController) BatchUploadImage(ctx *gin.Context) {
	formFiles, err := ctx.MultipartForm()
	if err != nil {
		control.BaseError(ctx, control.WithMessage("上传图片错误"))
		return
	}

	images := formFiles.File["images[]"]

	imagesUrl := []string{}
	for _, image := range images {
		imageExt := path.Ext(image.Filename)

		allowExtMap := map[string]bool{
			".jpg":  true,
			".png":  true,
			".gif":  true,
			".jepg": true,
		}

		if !allowExtMap[imageExt] {
			control.BaseError(ctx, control.WithMessage("图片类型错误仅支持jpg/png/gif/jepg"))
			return
		}

		filepath := "./static/upload_images/" + tools.GetDay()
		err = os.MkdirAll(filepath, 0755)
		if err != nil {
			control.BaseError(ctx, control.WithMessage("创建目录错误"))
			return
		}

		//获取微秒数
		// var filename string = ""
		var filename = strconv.FormatInt(tools.GetUnixNano(), 10)
		var h = md5.New()
		h.Write([]byte(filename)) // 需要加密的字符串
		filename = hex.EncodeToString(h.Sum(nil)) + imageExt
		filepath = filepath + "/" + filename

		// 上传文件到指定的路径
		err = ctx.SaveUploadedFile(image, filepath)
		if err != nil {
			control.BaseError(ctx, control.WithMessage("上传图片失败"))
			return
		}

		imagesUrl = append(imagesUrl, strings.TrimLeft(filepath, "."))
	}

	control.BaseSuccess(ctx, control.WithData(gin.H{
		"image_urls": imagesUrl,
	}), control.WithMessage("上传图片成功"))
}

/**
 * @description minio上传单张图片
 * @author huwl
 * @date 2025/08/05 19:42:09
 */
func (control ImageController) MinioSingleUploadImage(ctx *gin.Context) {
	image, err := ctx.FormFile("file")
	if err != nil {
		control.BaseError(ctx, control.WithMessage("请选择上传图片"))
		return
	}

	thumbs := []map[string]string{}
	water := map[string]string{}

	// thumbs = append(thumbs, map[string]string{
	// 	"width":  "200",
	// 	"height": "200",
	// })

	miu, err := tools.NewMinioImageUpload()
	if err != nil {
		control.BaseError(ctx, control.WithMessage(err.Error()))
		return
	}
	result, err := miu.MinioSingleUploadImage(ctx, image, thumbs, water)
	if err != nil {
		control.BaseError(ctx, control.WithMessage(err.Error()))
		return
	}

	control.BaseSuccess(ctx, control.WithData(gin.H{
		"image_url":          result["image_url"],
		"absolute_image_url": result["absolute_image_url"],
	}), control.WithMessage("上传图片成功"))
}
